<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" >
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>鲸拍注册</title>
    <link rel="stylesheet" href="/css/promote/style.css" />
    <link rel="stylesheet" href="/plugins/layer-v3.1.1/theme/default/layer.css" />
</head>
<body>
	<div id="test">
		<img src="/images/beijin.png"/>
		<!--<form action="" method="" name="form" id="form">-->
			<div class="center">
				<input type="text" placeholder="请输入账号（手机号）" id="phone" class="input"/>
				<input type="password" placeholder="请设置登录密码" id="psd" class="input"/>
				<input type="password" placeholder="请确认密码" id="Entpsd" class="input"/>
				<input type="text" placeholder="请输入短信验证码" class="inputNumber YZnumber" />
				<input type="button" class="YZbtn yzm" name="yzm" value="获取验证码" disabled="disabled">
				<!--<div class="input tj ">推荐人：<span id="span" th:text="${referralCode}"></span></div>-->
				<button id="btn">立即注册</button>
			</div>
		<!--</form>-->
		<footer>
			<img src="/images/footer.png"/>
		</footer>
	</div>
</body>
<script type="text/javascript" src="/plugins/jquery.min.js" ></script>
<script type="text/javascript" src="/plugins/layer-v3.1.1/layer.js" ></script>
<script type="text/javascript" th:inline="javascript">

	$("#btn").click(function(){
        var referral_code = /*[[${referralCode}]]*/ "";
	    var registerInfo={
	        "mobile_number":"",//手机号
			"password":"",//密码
			"referral_code":referral_code,//推荐人
			"sms_code":""//手机验证码
		};

		var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
		if($('#phone').val() == "" || $('#phone').val() == undefined){
			layer.msg('请输入账号（手机号）', {
			  time:2000
			});
		}else if(!myreg.test($('#phone').val())){
			layer.msg('请输入正确的手机号', {
			  time:2000
			});
		}else if($('#psd').val() == "" || $('#psd').val() == undefined ){
			layer.msg('请设置登录密码', {
			  time:2000
			});
		} else if ($('#psd').val().length<6) {
            layer.msg('密码不能低于6位', {
                time:2000
            });
		}else if($('#Entpsd').val() == "" || $('#Entpsd').val() == undefined ){
			layer.msg('请输入确认密码', {
			  time:2000
			});
        } else if($('#Entpsd').val() != $('#psd').val()){
			layer.msg('两次输入的密码不一致', {
			  time:2000
			});
		}else if($('.inputNumber').val() == "" || $('.inputNumber').val() == undefined){
			layer.msg('验证码不能为空', {
			  time:2000
			});
		}else{
            registerInfo.mobile_number = $('#phone').val();
            registerInfo.password = $('#Entpsd').val();
            registerInfo.sms_code = $('.inputNumber').val();
            $.ajax({
                type: "POST",
                url: "/users/register",
                contentType: "application/json;charset=UTF-8",
                data:JSON.stringify(registerInfo),
                dataType: "json",
                success: function (data) {
                    if (data.code && data.code==='success') {
                        window.location.href = "/pages/success.html"
                    } else {
                        layer.msg(data.message+"，请重试",{icon:2,time:1000});
					}
                },
                error: function (data) {
                    layer.msg(data.message,{icon:2,time:1000});
                }
            });
		}
	})
	$(document).ready(function() {
		var ordertime = 120;
		var timeleft = ordertime;
		var btn = $(".yzm");
		var phone = $("#phone");
		var reg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
		phone.keyup(function() {
			if(reg.test(phone.val())) {
				btn.removeAttr("disabled")
			} else {
				btn.attr("disabled", true)
			}
		})
		function timeCount() {
			timeleft -= 1;
			if(timeleft > 0) {
				btn.val(timeleft + " 秒后重发");
				setTimeout(timeCount, 1000)
			} else {
				btn.val("重新发送");
				timeleft = ordertime;
				btn.removeAttr("disabled");
			}
		}

        /**
		 * 发送短信验证码
         * @param tel 手机号码
         */
		function sendSms(tel) {
			$.ajax({
				type: "POST",
				url: "/mobile_phone/send_code",
				contentType: "application/json;charset=UTF-8",
				data:JSON.stringify({"mobile_number":tel,"code_type":"register","force_user_nonexist":true}),
				dataType: "json",
				success: function (data) {
                    if (data.code && data.code==='success') {
                        layer.msg("发送成功",{icon:1,time:1000});
                        $(this).attr("disabled", true);
                        timeCount(this);
                    } else {
                        layer.msg(data.message,{icon:2,time:1000});
                    }
                },
				error: function (data) {
                    layer.msg(data.message);
                }
			});
		}
		btn.on("click", function() {
			sendSms(phone.val());
		})
	})
	$(document).keydown(function(event) {
		if(event.keyCode == 13) {
			$("#btn").click();
		}
	});
</script>
</html>